/*
 * DocumentTestPanel.cpp
 *
 *  Created on: 2012-11-3
 *      Author: tutu
 */

#include "DocumentTestPanel.h"
#include "../TContext.h"
#include <QMessageBox>

DocumentTestPanel::DocumentTestPanel(QWidget *parent) : QWidget(parent)
{
	_mineButton = new QPushButton(this);
	_mineButton->setText("List Mine");
	_mineButton->setGeometry(10, 10, 100, 30);
	connect(_mineButton, SIGNAL(clicked()), this, SLOT(mineButtonClicked()));

	_favoritedButton = new QPushButton(this);
	_favoritedButton->setText("List Favorited");
	_favoritedButton->setGeometry(10, 50, 100, 30);
	connect(_favoritedButton, SIGNAL(clicked()), this, SLOT(favoritedButtonClicked()));

	_latestUsedButton = new QPushButton(this);
	_latestUsedButton->setText("List Latest Used");
	_latestUsedButton->setGeometry(10, 90, 100, 30);
	connect(_latestUsedButton, SIGNAL(clicked()), this, SLOT(latestUsedButtonClicked()));

	_sharedButton = new QPushButton(this);
	_sharedButton->setText("List Shared");
	_sharedButton->setGeometry(10, 130, 100, 30);
	connect(_sharedButton, SIGNAL(clicked()), this, SLOT(sharedButtonClicked()));

	_idEdit = new QLineEdit(this);
	_idEdit->setText("Id List");
	_idEdit->setGeometry(10, 170, 150, 30);

	_deleteButton = new QPushButton(this);
	_deleteButton->setText("Delete");
	_deleteButton->setGeometry(10, 210, 100, 30);
	connect(_deleteButton, SIGNAL(clicked()), this, SLOT(deleteButtonClicked()));

	_queryEdit = new QLineEdit(this);
	_queryEdit->setText("Query");
	_queryEdit->setGeometry(10, 250, 150, 30);

	_searchButton = new QPushButton(this);
	_searchButton->setText("Search");
	_searchButton->setGeometry(10, 290, 100, 30);
	connect(_searchButton, SIGNAL(clicked()), this, SLOT(searchButtonClicked()));

	_requester = TContext::instance()->requesterFactory()->createDocumentRequester();
	connect(_requester.data(), SIGNAL(requestFinished(bool, QList<TDocumentPointer>)), this, SLOT(requestFinished(bool, QList<TDocumentPointer>)));
	connect(_requester.data(), SIGNAL(deleteFinished(bool)), this, SLOT(deleteFinished(bool)));

	_searchRequester = TContext::instance()->requesterFactory()->createSearchRequester();
	connect(_searchRequester.data(), SIGNAL(searchFinished(bool, QList<TDocumentPointer>)), this, SLOT(requestFinished(bool, QList<TDocumentPointer>)));
}

DocumentTestPanel::~DocumentTestPanel()
{
	// TODO Auto-generated destructor stub
}

void DocumentTestPanel::mineButtonClicked()
{
	_requester->requestMine();
}

void DocumentTestPanel::favoritedButtonClicked()
{
	_requester->requestFavorited();
}

void DocumentTestPanel::latestUsedButtonClicked()
{
	_requester->requestLatestUsed(10);
}

void DocumentTestPanel::sharedButtonClicked()
{
	_requester->requestShared();
}

void DocumentTestPanel::deleteButtonClicked()
{
	if(_idEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No ids");
	}
	else
	{
		QStringList temp = _idEdit->text().split(",");
		if(temp.size()==1)
		{
			_requester->deleteDocument(temp.at(0).toInt());
		}
		else
		{
			QList<int> idList;
			for(int i=1; i<=temp.size(); i++)
			{
				idList.append(temp.at(i-1).toInt());
			}
			_requester->deleteDocuments(idList);
		}
	}
}

void DocumentTestPanel::searchButtonClicked()
{
	if(_queryEdit->text().isEmpty())
	{
		QMessageBox::warning(NULL, "Error", "No query");
	}
	else
	{
		_searchRequester->search(_queryEdit->text());
	}
}

void DocumentTestPanel::requestFinished(bool error, QList<TDocumentPointer> documents)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "List Success. Doc number is "+QString::number(documents.size()));
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "List Failure.");
	}
}

void DocumentTestPanel::deleteFinished(bool error)
{
	if(!error)
	{
		QMessageBox::warning(NULL, "Result", "Delete Success.");
	}
	else
	{
		QMessageBox::warning(NULL, "Result", "Delete Failure.");
	}
}
